<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="用户名称">
              <a-input placeholder="请输入" v-model="queryParam.key"/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="状态">
              <a-select placeholder="请选择" default-value="0" v-model="queryParam.isEnable">
                <a-select-option value="">全部</a-select-option>
                <a-select-option value="1">正常</a-select-option>
                <a-select-option value="2">禁用</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <span class="table-page-search-submitButtons">
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button style="margin-left: 8px" @click="() => queryParam = { key:null,isEnable: null}">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <div class="table-operator">
      <a-button type="primary" icon="plus" @click="handleEdit()">新建</a-button>
    </div>

    <s-table
      ref="table"
      size="small"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
    >
      <!-- <div
        slot="expandedRowRender"
        slot-scope="record"
        style="margin: 0">
        <a-row
          :gutter="24"
          :style="{ marginBottom: '12px' }"> -->
          <!-- <a-col :span="12" v-for="(role, index) in record.permissions" :key="index" :style="{ marginBottom: '12px' }">
            <a-col :span="4">
              <span>{{ role.permissionName }}：</span>
            </a-col>
            <a-col :span="20" v-if="role.actionEntitySet.length > 0">
              <a-tag color="cyan" v-for="(action, k) in role.actionEntitySet" :key="k">{{ action.describe }}</a-tag>
            </a-col>
            <a-col :span="20" v-else>-</a-col>
          </a-col> -->
        <!-- </a-row>
      </div> -->
      <span slot="action" slot-scope="text, record">
        <a @click="handleEdit(record)">编辑</a>
        <a-divider type="vertical" />
        <a-dropdown>
          <a class="ant-dropdown-link">
            更多 <a-icon type="down" />
          </a>
          <a-menu slot="overlay" >
            <a-menu-item>
              <a @click="generatePassword(record.id)">重置密码</a>
            </a-menu-item>
            <a-menu-item>
              <a @click="editDeleted(record.id)">删除</a>
            </a-menu-item>
          </a-menu>
        </a-dropdown>
        <!-- <a-dropdown v-model="record.dropMenuVisible">
          <a class="ant-dropdown-link">
            更多 <a-icon type="down" />
          </a>
          <a-menu slot="overlay" @click="menuItemClick">
            
          </a-menu>
        </a-dropdown> -->
      </span>
    </s-table>

    <a-modal :width="640" :visible="visible" title="密码重置" @ok="visible = false" @cancel="visible = false">
      <a-form @submit="visible = false" :form="form">
        <a-form-item
          label="您重置的密码为"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input v-decorator="['respPassword']" />
        </a-form-item>
      </a-form>
    </a-modal>
  </a-card>
  
</template>

<script>
import STable from '@/components/Table'
import MoreOptions from '@/components/MoreOptions'
import {getAdminUserList, generatePassword, deleteUser, enableUser} from "../../api/auth";

export default {
  name: 'TableList',
  components: { STable, MoreOptions},
  data () {
    return {
      // description: '列表使用场景：后台管理中的权限管理以及角色管理，可用于基于 RBAC 设计的角色权限控制，颗粒度细到每一个操作类型。',
      visible: false,
      form: this.$form.createForm(this),
      mdl: {},
      // 查询参数
      queryParam: {
        key: null,
        isEnable: null
      },
      // 表头
      columns: [
        {
          title: '用户名',
          dataIndex: 'username'
        },
        {
          title: '姓名',
          dataIndex: 'name'
        },
        {
          title: '联系电话',
          dataIndex: 'phone'
        },
        {
          title: '状态',
          dataIndex: 'isEnable',
          customRender: (text) => { if (text == 1) return '正常'; else '禁用' }
        },
        {
          title: '备注',
          dataIndex: 'remark',
        }, {
          title: '操作',
          width: '150px',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        console.log('[loadData parameter]',parameter);
        return getAdminUserList(Object.assign(parameter, this.queryParam)).then(res => {
          return res.data
        })
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      },
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  methods: {
    handleEdit (record) {
      if(!record) this.$router.push({name: 'AdminUserDetail'})
      else this.$router.push({name: 'AdminUserDetail',query: {id: record.id}})

      // this.mdl.permissions.forEach(permission => {
      //   permission.actionsOptions = permission.actionEntitySet.map(action => {
      //     return { label: action.describe, value: action.action, defaultCheck: action.defaultCheck }
      //   })
      // })
      // console.log(this.mdl)
      // this.visible = true
    },
    async editDeleted(id,) {
      let data = {id: id,}
      let resp = await deleteUser(data)
      if (this.validResp(resp)) {
        this.$message.success('删除成功!');
        this.$refs.table.refresh()
      } else {
        this.$message.warning(resp.msg||'删除失败!');
      }
    },
    async generatePassword(id) {
      this.visible = true
      let resp = await generatePassword({id: id})
      if (this.validResp(resp)) {
        this.form.setFieldsValue({respPassword:resp.data.password})
      } else this.$message.warning(resp.msg);
    },
    onChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
  },
  watch: {
    /*
      'selectedRows': function (selectedRows) {
        this.needTotalList = this.needTotalList.map(item => {
          return {
            ...item,
            total: selectedRows.reduce( (sum, val) => {
              return sum + val[item.dataIndex]
            }, 0)
          }
        })
      }
      */
  }
}
</script>
